#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <sstream>
#include <fstream>

using namespace std;

class TextQuery{
public:
    void readFile(const string & filename){
        ifstream ifs(filename);
        if(!ifs.good()){
            cout << "eorror" << endl;
            return;
        }
        string line;
        int linenumber = 0;
        while(getline(ifs,line)){
            ++linenumber;
            _lines.push_back(line);
            istringstream iss(line);
            string word;
            while(iss >> word){
                _wordNumbers[word].insert(linenumber);
                ++_wordCount[word];
            }
        }
    }
    void query(const string & word){
        cout << "---------------------"<< endl;
        cout << word << " occurs " << _wordCount[word] << " times " << endl;
        for(auto &ele:_wordNumbers[word]){
            cout <<"(line " << ele << ")" << _lines[ele - 1] << endl;
        }
        cout << "---------------------"<< endl;
    }


private:
    //每一行的数据
    vector<string> _lines;
    //每个单词对应的行号
    map<string, set<int>> _wordNumbers;
    //每个单词出现的总次数
    map<string, int> _wordCount;
};


void test(){
    TextQuery tq;
    tq.readFile("china_daily.txt");
    tq.query("Shenzhen");
}

int main()
{
    test();
    return 0;
}

